﻿var DELIMITER = "]-[";
var NEWLINE = "]=[";
var LEFTBRACE = "|#[";
var RIGHTBRACE = "]#|";
var SLASH = "|#|";
var prefix = "ctl00_content_"; // hanya untuk element yang pake runat="server"

        
var LOADING= new Image(50,10); 
LOADING.src="images/loading_rainbow.gif";

var DEFAULT_AVATAR= new Image(50,50); 
DEFAULT_AVATAR.src="upload/company/-99.png";

var DEFAULT_USER= new Image(50,50); 
DEFAULT_USER.src="upload/avatar/-99.png";

var LIPETAN_FOLD= new Image(20,26); 
LIPETAN_FOLD.src="images/nav_lipetan.png";

var LIPETAN_UNFOLD= new Image(20,26); 
LIPETAN_UNFOLD.src="images/nav_lipetan2.png";

trim = function(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
ltrim = function(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
rtrim = function(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
}

String.prototype.replaceBraceTag = function(){
    return this.replace("<", LEFTBRACE).replace(">", RIGHTBRACE).replace("/",SLASH);
}

String.prototype.convertToBraceTag = function(){
    return this.replace(LEFTBRACE, "<").replace(RIGHTBRACE, ">").replace(SLASH,"/");
}

selfClose = function()
{
    window.close();
}

$id = function(id)
{
    return document.getElementById(id);
}

$nm = function(nm, div, tagname)
{
    if( div == undefined )
    {
        return document.getElementsByName(nm);
    }
    else
    {
        var arr = div.getElementsByTagName(tagname);
        var arRet = new Array();
        
        var iCount = 0;
        
        for( var i=0; i < arr.length; i ++ )
        {
            if( arr[i].getAttribute("name") == nm )
            {
                arRet[iCount] = arr[i];
                iCount++;
            }
        }
        
        return arRet;
    }
}

redirectTo = function(url){
    window.location = url;
}

//setup_MCE = function()
//{
//    tinyMCE.init({
//        mode : "textareas",
//        theme : "advanced",
//        plugins: "media",
//        plugins : "table,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,visualchars,nonbreaking",
//        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,forecolor,backcolor",
//        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,undo,redo,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,hr,removeformat,visualaid,|,sub,sup",
//        theme_advanced_buttons3 : "tablecontrols,|,link,unlink,image,cleanup,|,insertdate,inserttime,|,preview, fullscreen",
//        disk_cache : true,
//        theme_advanced_toolbar_location : "top",
//        theme_advanced_toolbar_align : "left",
//        theme_advanced_statusbar_location : "bottom",
//        theme_advanced_resizing : true
//    });
//}

renderFirst = function()
{
    setTimeout("doNothing()", 0);
}

closePopupLayer = function()
{
    var div = $id("popuplayer");    div.innerHTML = "";    div.style.display = "none";    renderFirst();
}

doNothing = function()
{}

onlyNumbers = function(elmt,e){
    var charCode;
    
    if(window.event)
    charCode = window.event.keyCode; // IE
    else
    charCode = e.which; // Firefox

    //var e = event || evt; // for trans-browser compatibility
    //var charCode = e.which || e.keyCode;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

onlyDecimals = function(elmt,e){
    var charCode;
    
    if(window.event)
    charCode = window.event.keyCode; // IE
    else
    charCode = e.which; // Firefox
    //var e = event || evt; // for trans-browser compatibility
    //var charCode = e.which || e.keyCode;
    
    if(charCode == 44 && elmt.value.replace(",","") != elmt.value )
        return false;
    else if(charCode == 44)
        return true;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

formatDecimals = function(elmt){
    var splitted = elmt.value.split(",");
    var hasComma = elmt.value.replace(",","") != elmt.value;
    var result = FormatDecimal(splitted[0].replace(".",""));
    
    if( hasComma )
    {
        result += ",";
    }
    
    if( splitted.length > 1 )
    {
        result += splitted[1];
    }
    
    elmt.value = result;
}


FormatDecimal = function(value) {
   a = value;
   b = a.replace(/[^\d]/g,"");
   c = "";
   panjang = b.length;
   j = 0;
   for (i = panjang; i > 0; i--) {
     j = j + 1;
     if (((j % 3) == 1) && (j != 1)) {
       c = b.substr(i-1,1) + "." + c;
     } else {
       c = b.substr(i-1,1) + c;
     }
   }
   return c;
}

onlyEmail = function(elmt){
    var IsValid = validateEmail(elmt.value) || elmt.value == "";
    if(IsValid)
    {
        elmt.style.backgroundColor = "#FFFFFF";
    }
    else
    {
        elmt.style.backgroundColor = "#FFAAAA";
        elmt.focus();
    }
    
    return IsValid;
}

validateEmail = function(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
}

onlyPhone = function(elmt){
    var IsValid = validatePhone(elmt.value) || elmt.value == "";
    if(IsValid)
    {
        elmt.style.backgroundColor = "#FFFFFF";
    }
    else
    {
        elmt.style.backgroundColor = "#FFAAAA";
        elmt.focus();
    }
    
    return IsValid;
}

validatePhone = function(elementValue){  
   var phonePattern = /^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/;  
   return phonePattern.test(elementValue);  
}

testRegex = function(value, regexs){
    var myregexp = new RegExp(regexs);
    return myregexp.test(value);
}

regexTextbox = function(elmt, regexString){
    var IsValid = testRegex(elmt.value, regexString) || elmt.value == "";
    if(IsValid)
    {
        elmt.style.backgroundColor = "#FFFFFF";
    }
    else
    {
        elmt.style.backgroundColor = "#FFAAAA";
        elmt.focus();
    }
    
    return IsValid;
}

toggleCheckbox = function(id){
    var checkbox = $id(id);
    checkbox.checked = !checkbox.checked;
}

expandMenu = function(div){
    var allsubmenus = $nm("sbmnu", document, "div");
    var thissubmenus = $nm("sbmnu", div.parentNode, "div");
    
    var alllipetan = $nm("imfld");
    var thislipetan = $nm("imfld", div.parentNode, "img");
    
    for( var i=0; i < allsubmenus.length; i++ )
    {
        //allsubmenus[i].className = "m_navSubmodul_link";
        allsubmenus[i].style.height = "0px";
    }
    
    for( var x=0; x < alllipetan.length; x++ )
    {
        alllipetan[x].src = LIPETAN_FOLD.src;
    }    
    
    renderFirst();
    
    for( var j=0; j < thissubmenus.length; j++ )
    {
        var ht = 0;
        while( ht < 26 )
        {
            ht += 2;
            thissubmenus[j].style.height = ht + "px";
            renderFirst();
        }
    }
    
    for( var y=0; y < thislipetan.length; y++ )
    {
        thislipetan[y].src = LIPETAN_UNFOLD.src;
    }
    
}

setEmptyCompanyAvatar = function(elmt){
    elmt.src = DEFAULT_AVATAR.src;
    elmt.onerror = ""; 
    return true;
}

setEmptyUserAvatar = function(elmt){
    elmt.src = DEFAULT_USER.src;
    elmt.onerror = ""; 
    return true;
}